
#include <cstdio>
#include <BMap>
#include <BFile>
#include <BSpring>
#include <BSpacer>
#include <BApplication>

#include "ReadWrite_obc.h"

ReadWrite_obc::ReadWrite_obc()
{
	_author = "zym";
	_version = "1.0";
	_description = "This is BWE ReadWrite Plugin for *.obc file.";
}
ReadWrite_obc::~ReadWrite_obc()
{

}

void ReadWrite_obc::setOptions(const BString& options)
{
	_options = options;
}
const BString& ReadWrite_obc::options() const
{
	return _options;
}

const BString& ReadWrite_obc::author() const
{
	return _author;
}
const BString& ReadWrite_obc::version() const
{
	return _version;
}
const BString& ReadWrite_obc::description() const
{
	return _description;
}
const BString& ReadWrite_obc::message() const
{
	return _document.message();
}

BObject* ReadWrite_obc::load(const BString& fileName)
{
	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		printf("OBC Error - open file failed : %s\n", fileName.cstr());
		return 0;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	BObject* object = _document.read(file);
	BApplication::SetCurrentPath(currentPath);

	return object;
}
bool ReadWrite_obc::load(const BString& fileName, BObject* object)
{
	if (!object)
		return false;

	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		printf("OBC Error - open file failed : %s\n", fileName.cstr());
		return false;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	bool result = _document.read(file, object);
	BApplication::SetCurrentPath(currentPath);

	return result;
}
bool ReadWrite_obc::save(const BString& fileName, const BObject* object) const
{
	if (!object)
		return false;

	BFile file(fileName);
	if (!file.open(IO_Write))
	{
		printf("OBC Error -- open file failed : %s\n", fileName.cstr());
		return false;
	}

	BString currentPath = BApplication::CurrentPath();
	BApplication::SetCurrentPath(fileName.path());
	bool result = _document.write(file, object);
	BApplication::SetCurrentPath(currentPath);

	return result;
}

BObject* ReadWrite_obc::read(const BBuffer& buffer)
{
	return _document.read(buffer);
}
bool ReadWrite_obc::read(const BBuffer& buffer, BObject* object)
{
	if (!object)
		return false;
	bool result = _document.read(buffer, object);
	return result;
}
bool ReadWrite_obc::write(BBuffer& buffer, const BObject* object) const
{
	if (!object)
		return false;
	bool result = _document.write(buffer, object);
	return result;
}

